<template>
    <div class="login-container" v-loading="loading" element-loading-text="拼命加载中">
        <div class="box_div">
            <div class="left">
                <h2 class="box_h2">Welcome to</h2>
                <h2 class="box_h2">Tianhai Space Positioning System</h2>
                <p>数据采集 | 存储计算 | 工艺组态 </p>
                <p>工业应用一体化物联网平台</p>
            </div>
            <div class="right">
                <el-form ref="loginForm" :model="loginForm" :rules="infoFormRules" class="login-form" auto-complete="on"
                    label-position="left">
                    <div class="title-container">
                        <h2 class="title">注册</h2>
                        <p>注册试用申请</p>
                    </div>

                    <el-form-item prop="userName">
                        <span class="svg-container" style="color: red">
                            <!-- <svg-icon icon-class="user" /> -->
                            *
                        </span>
                        <el-input ref="userName" v-model="loginForm.userName" placeholder="用户名" name="userName"
                            type="text" tabindex="1" size="mini" auto-complete="on" />
                    </el-form-item>

                    <el-form-item prop="password">
                        <span class="svg-container" style="color: red">
                            <!-- <svg-icon icon-class="password" /> -->
                            *
                        </span>
                        <el-input size="mini" ref="password" v-model.trim="loginForm.password" type="text"
                            placeholder="密码" name="password" tabindex="2" auto-complete="on"
                            @keyup.enter.native="handleLogin" />
                    </el-form-item>
                    <el-form-item>
                        <!-- <span class="svg-container" style="color: red">
                            *
                        </span> -->
                        <el-input size="mini" ref="nickName" v-model="loginForm.nickName" type="text" placeholder="昵称"
                            name="nickName" tabindex="2" auto-complete="on" @keyup.enter.native="handleLogin" />
                    </el-form-item>
                    <el-form-item>
                        <el-input size="mini" ref="mobilePhone" v-model="loginForm.mobilePhone" type="text"
                            placeholder="手机号" name="mobilePhone" tabindex="2" auto-complete="on"
                            @keyup.enter.native="handleLogin" />
                    </el-form-item>
                    <el-form-item>
                        <el-input size="mini" ref="email" v-model="loginForm.email" type="text" placeholder="邮箱"
                            name="email" tabindex="2" auto-complete="on" @keyup.enter.native="handleLogin" />
                    </el-form-item>

                    <el-button :loading="loading" type="primary" style="width: 100%; margin-bottom: 20px"
                        @click.native.prevent="handleLogin">提交申请</el-button>
                    <el-button :loading="loading" style="width: 100%; margin-bottom: 30px; margin-left: 0px;"
                        @click="$router.push('/login')">返回登录</el-button>
                </el-form>
            </div>
        </div>
    </div>
</template>

<script>
    import { validUsername } from "@/utils/validate";
    import { addUser, login } from "@/api/user";
    import { ADMINUSERNAME, ADMINUSERPASSWORD, USERNAME, USERPASSWORD } from "@/data/user";

    export default {
        name: "CodeLogin",
        data() {
            return {
                loginForm: {
                    userName: "",
                    password: "",
                    nickName: "",
                    mobilePhone: "",
                    email: "",
                    avatar: "",
                },
                infoFormRules: {
                    // 用户名
                    userName: [
                        { required: true, message: "请输入用户名", trigger: "blur" }
                    ],
                    // 密码
                    password: [
                        { required: true, message: "请输入密码", trigger: "blur" }
                    ],
                    // 昵称
                    nickName: [
                        { required: true, message: "请输入昵称", trigger: "blur" }
                    ],
                    // 手机号
                    mobilePhone: [
                        { required: true, message: "请输入手机号", trigger: "blur" },
                        {
                            pattern: /^1[3-9][0-9]\d{8}$/,
                            message: "请输入正确的手机号格式",
                            trigger: "blur"
                        }
                    ],
                    // 邮箱
                    email: [
                        { required: true, message: "请输入邮箱", trigger: "blur" },
                        {
                            pattern: /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/,
                            message: "请输入正确的邮箱格式",
                            trigger: "blur"
                        }
                    ],
                },
                loading: false,
                redirect: undefined,
            };
        },
        watch: {
            $route: {
                handler: function (route) {
                    this.redirect = route.query && route.query.redirect;
                },
                immediate: true,
            },
        },
        mounted() {
            localStorage.setItem("token", "");
        },
        methods: {
            // 注册
            async addUser() {
                try {
                    const { code } = await addUser(this.loginForm);
                    if (code == 0) {
                        // this.$message.success("创建成功");
                        await this.login();
                    }
                } catch (error) {
                    console.log(error);
                }
            },
            // 登录
            async login() {
                if (this.loginForm.username == "shutong" && this.loginForm.password == "js") {
                    this.$message.success("登录成功");
                    localStorage.setItem("token", "token");
                    // 用户权限
                    localStorage.setItem("userType", "admin");
                    this.$router.push({ path: this.redirect || '/' });
                    this.loading = false;
                } else if (this.loginForm.username == "tianhai" && this.loginForm.password == "js") {
                    this.$message.success("登录成功");
                    localStorage.setItem("token", "token");
                    // 用户权限
                    localStorage.setItem("userType", "ordinary");
                    this.$router.push({ path: this.redirect || '/' });
                    this.loading = false;
                } else {
                    try {
                        const data = {
                            username: this.loginForm.userName,
                            password: this.loginForm.password
                        };
                        const { code, result } = await login(data);
                        if (code == 0) {
                            localStorage.setItem("userType", "ordinary");
                            this.$message.success(result);
                            localStorage.setItem("token", "token");
                            this.$router.push({ path: this.redirect || '/' });
                            this.loading = false;
                        }
                    } catch (error) {
                        this.loading = false;
                        console.log(error);
                    }
                }
            },
            showPwd() {
                if (this.passwordType === "password") {
                    this.passwordType = "";
                } else {
                    this.passwordType = "password";
                }
                this.$nextTick(() => {
                    this.$refs.password.focus();
                });
            },
            handleLogin() {
                this.$refs.loginForm.validate(async (valid) => {
                    if (valid) {
                        this.loading = true;
                        if (this.loginForm.userName == ADMINUSERNAME && this.loginForm.password == ADMINUSERPASSWORD) {
                            this.$message.success("登录成功");
                            localStorage.setItem("token", "token");
                            // 用户权限
                            localStorage.setItem("userType", "admin");
                            this.$router.push({ path: this.redirect || '/' });
                            this.loading = false;
                        } else if (this.loginForm.username == SHUTONGADMINUSERNAME && this.loginForm.password == SHUTONGADMINPASSWORD) {
                            this.$message.success("登录成功");
                            localStorage.setItem("token", "token");
                            // 用户权限
                            localStorage.setItem("userType", "admin_plus");
                            this.$router.push({ path: this.redirect || '/' });
                            this.loading = false;
                        } else if (this.loginForm.userName == USERNAME && this.loginForm.password == USERPASSWORD) {
                            this.$message.success("登录成功");
                            localStorage.setItem("token", "token");
                            // 用户权限
                            localStorage.setItem("userType", "ordinary");
                            this.$router.push({ path: this.redirect || '/' });
                            this.loading = false;
                        } else {
                            await this.addUser();
                        }
                    } else {
                        console.log("error submit!!");
                        return false;
                    }
                });
            },
        },
    };
</script>

<style lang="scss">
    /* 修复input 背景不协调 和光标变色 */
    /* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

    $bg: #fff;
    $light_gray: #fff;
    $cursor: #000;

    @supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
        .login-container .el-input input {
            color: $cursor;
        }
    }

    /* reset element-ui css */
    .login-container {
        .el-form-item__content {
            height: 40px;
            line-height: 26px;
        }

        .el-input {
            display: inline-block;
            height: 47px;
            width: 85%;

            input {
                background: transparent;
                border: 0px;
                border-radius: 0px;
                padding: 12px 5px 12px 15px;
                color: #000;
                height: 40px;
                caret-color: #000;

                &:-webkit-autofill {
                    box-shadow: 0 0 0px 1000px $bg inset !important;
                    -webkit-text-fill-color: $cursor !important;
                }
            }
        }

        .el-form-item {
            border: 1px solid #eee;
            background: #fff;
            border-radius: 5px;
            color: #454545;
        }
    }
</style>

<style lang="scss" scoped>
    $bg: #2d3a4b;
    $dark_gray: #889aa4;
    $light_gray: #eee;

    .login-container {
        min-height: 100%;
        width: 100%;
        // background-color: $bg;
        // background: url(../../assets/img/bg_img1.png);
        background: url(../../assets/img/logo_box.jpg);
        background-repeat: no-repeat;
        background-size: cover;
        overflow: hidden;

        &>div {
            // width: 1128px;
            // height: 695px;
            width: 57%;
            height: 70%;
            opacity: 1;
            border-radius: 8px;
            background: rgba(255, 255, 255, 1);
            // box-shadow: 0px 10px 20px 0px rgba(219, 231, 238, 1);
            position: fixed;
            left: 50%;
            // top: 50%;
            top: 53%;
            transform: translateX(-50%) translateY(-50%);
            box-sizing: border-box;
            display: flex;

            .left {
                width: 432px;
                height: 100%;
                background-color: rgba(24, 144, 255, 1);
                text-align: center;
                padding-top: 112px;
                box-sizing: border-box;
                color: #fff;

                &>h2 {
                    margin-top: 0;
                    font-size: 30px;
                }

                &>p {
                    font-size: 16px;
                }
            }
        }

        .login-form {
            position: relative;
            width: 520px;
            max-width: 100%;
            padding: 145px 35px 0 132px;
            box-sizing: border-box;
            margin: 0 auto;
            overflow: hidden;
        }

        .svg-container {
            padding: 6px 5px 6px 15px;
            color: $dark_gray;
            vertical-align: middle;
            width: 30px;
            display: inline-block;
        }

        .title-container {
            position: relative;

            .title {
                font-size: 20px;
                color: rgba(0, 0, 0, 1);
                margin: 0px auto 40px auto;
                font-weight: bold;
                margin-bottom: 0;
            }

            &>p {
                margin-top: 8px;
                color: rgba(128, 128, 128, 1);
            }
        }

        .show-pwd {
            position: absolute;
            right: 10px;
            top: 7px;
            font-size: 16px;
            color: $dark_gray;
            cursor: pointer;
            user-select: none;
        }
    }

    @media screen and (max-width: 1740px) {
        .box_h2 {
            font-size: 24px !important;
        }
    }

    @media screen and (max-height: 898px) {
        .login-container .login-form {
            padding: 85px 35px 0px 132px !important;
        }
    }

    @media screen and (max-height: 800px) {
        .login-container>div {
            height: 562px !important;
        }
    }

    @media screen and (max-width: 1521px) and (max-height: 776px) {
        .login-container>div {
            width: 866px !important;
            height: 562px !important;
        }
    }
</style>